package com.hentai.androidserver.service.impl;

import com.hentai.androidserver.domain.Users;
import com.hentai.androidserver.mapper.UsersMapper;
import com.hentai.androidserver.service.UsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Service
public class UserServiceImpl implements UsersService {

    @Resource
    UsersMapper usersMapper;

    @Override
    public Users login(Users users) {
        users.setPassword(encode(users.getPassword()));
        return usersMapper.login(users);
    }

    @Override
    public void register(Users users) {
        users.setPassword(encode(users.getPassword()));
        if (users.getPassword() == null || users.getPassword().equals("")) {
            return;
        }
        usersMapper.register(users);
    }

    private String encode(String src) {

        if (src == null || src.equals("")) {
            return null;
        }

        return Base64.getEncoder().encodeToString(src.getBytes(StandardCharsets.UTF_8));
    }
}
